/*This program calculates your profit/loss for all your
 *investments in one share.
 *Use a positive number for purchase, and a negative for sell. .
 *Enter zero (0) shares to exit the program.
 */

#include <iostream>
#include <iomanip>

int main (int argc, const char* argv[])
{
	std::cout << "This program calculates your profit/loss for all your" << std::endl;
	std::cout << "investments in one share." << std::endl;
    std::cout << "Use a positive number for purchase, and a negative for sell." << std::endl;
    std::cout << "Enter zero (0) shares to exit the program." << std::endl  << std::endl;

    int numberTransaction = 1;
    int numberShares = 0;
    float price = 0;
    float totalPrice = 0;
    float totalShares = 0;

    do {
    	std::cout << "Enter data for transaction " << numberTransaction<< std::endl;
        do {
        	std::cout << "Number of shares : ";
            std::cin >> numberShares;
            if ((numberShares + totalShares ) < 0 ){
            	std::cout << "You do not have that many shares, try again." << std::endl;
            }
        } while ((numberShares < 0) && (numberShares + totalShares ) < 0);
          if (numberShares != 0){
        do {
            std::cout << "Price per share  : ";
            std::cin >> price;
            if (price < 0){
            	std::cout << "The price may not be negative, try again." << std::endl;
             }
        } while (price < 0);

        if (numberShares < 0 ) {
        	 std::cout << std::setprecision(2) << std::fixed << "You spent  $" << (price*(numberShares - (numberShares*2)))
        	            		  << " on this transaction." << std::endl;
        } else {
        	std::cout << std::setprecision(2) << std::fixed << "You spent  $" << price*numberShares
        	        	            		  << " on this transaction." << std::endl;
        }
            totalPrice  += price*numberShares;
            totalShares += numberShares;

            std::cout << "Your total spendings amount to $" << totalPrice << " and you own " << std::setprecision(0)
            		<< totalShares << " shares." << std::endl << std::endl;
            ++numberTransaction;
        } else
        	std::cout << "Bye. Have a nice day."  << std::endl;
    }while ( numberShares != 0 );
}
